<?php
namespace Home\Controller;
class FreeAgreementController extends CommonController{
	
	public function _initialize(){
		parent::_initialize();
		$this->add_step('免费合同', URL('index'));
	}
	// 免费合同主页
	public function index(){
		// 热门关键字
		$this->assign('hot_keyword', D('Keyword')->get_hot_list('free_agreement'));
		// 创业合同类别
		$this->assign('venture_agreement_sort', D('Category')->get_list('venture_agreement'));
		// 企业合同类别
		$this->assign('enterprise_agreement_sort', D('Category')->get_list('enterprise_agreement'));
		// 个人合同类别
		$this->assign('personal_agreement_sort', D('Category')->get_list('personal_agreement'));
		
		// 排序
		$order_by = $this->_get('order_by');
		// 按最新在前面
		if($order_by == 'new'){
			$order['create_time'] = 'desc';
		}
		// 按下载量
		if($order_by == 'download_num'){
			$order['download_num'] = 'desc';
		}
		D('FreeAgreement')->pagesize = 8;
		$list  = D('FreeAgreement')->get_list('', $order, $this->_get('p', '', 1));
		$count = D('FreeAgreement')->count;
		//处理AJAX分页
		$this->page($list, $count);
		$this->assign('list', $list);
		$this->display();
	}
	// 搜索页面
	public function search(){
		$keyword = $this->_get('keyword');
		if($keyword){
			$where['title|description|keyword'] = array('like', '%' . $keyword . '%');
		}
		$category_id = $this->_get('category_id');
		if($category_id){
			$where['category_id']  = $category_id;
			$params['category_id'] = $category_id;
			$category_name = D('Category')->get_name($category_id);
		}
		$type = $this->_get('type');
		if($type){
			$where['type']  = $type;
			$params['type'] = $type;
			$type_name = get_type_desc($type, 'free_agreement_type_desc');
		}
		if($type_name){
			$this->add_step($type_name, U('search', array('type' => $type)));
		}
		if($category_name){
			$this->add_step($category_name);
		}
		if(!$type_name && !$category_name){
			$this->is_nav_hide = true;
		}
		$list  = D('FreeAgreement')->get_list($where, '', $this->_get('p', '', 1));
		$count = D('FreeAgreement')->count;
		//处理AJAX分页
		$this->page($list, $count);

		// 热门关键字
		$this->assign('hot_keyword', D('Keyword')->get_hot_list('free_agreement'));
		$this->assign('list', $list);
		$this->assign('params', $params);
		$this->assign('count', $count);
		$this->assign('keyword', $keyword);
		$this->assign('category_id', $category_id);
		$this->display();
	}
	// 合同详情
	public function detail(){
		// 获取合同信息
		$agreement_id = $this->_get('agreement_id', '对不起，您的操作有误！');
		$info = D('FreeAgreement')->info($agreement_id);
		$this->add_step($info['title']);
		if(!$info['agreement_id']){
			$this->error('对不起，合同不存在！');
		}
		// 清空用户问答数据
		D('FreeAgreement')->clear_ask_answer($this->user_id, $agreement_id);
		// 获取合同内容
		$doc_content = D('FreeAgreement')->get_doc_content($this->user_id, $agreement_id);

		$this->assign('doc_content', $doc_content);
		$this->assign('info', $info);
		$this->display();
	}
	// 生成合同步骤
	public function step(){
		// 不显示头部导航
		$this->is_show_header_nav = 0;
		// 获取合同信息
		$agreement_id = $this->_get('agreement_id', '对不起，您的操作有误！');

		$info = D('FreeAgreement')->info($agreement_id);
		$this->add_step($info['title']);
		$this->add_step('创建合同');
		if(!$info['agreement_id']){
			$this->error('对不起，合同不存在！');
		}

		// 获取步骤信息
		$step = $this->_get('step', '', 1);
		$step_info = D('FreeAgreement')->get_step_info($agreement_id, $step);
		$step_num  = $step_info['step_num'];
		$next_step = $step_num >= $step ? $step + 1 : '';
		if(!$step_info['ask_data'] && $step <= $step_num){
			$this->error('此合同尚未配置！');
		}
		// 获取保存数据
		$cache_answer = D('FreeAgreement')->get_ask_answer($this->user_id, $agreement_id);
		if(IS_POST){
			$answer = $this->_post('answer');
			D('FreeAgreement')->set_ask_answer($this->user_id, $agreement_id, $answer);
			if($step > $step_num){
				redirect(U('finish', array('agreement_id' => $agreement_id)));
			}
		}
		$doc_content = D('FreeAgreement')->get_doc_content($this->user_id, $agreement_id);

		$this->assign('info', $info);
		$this->assign('ask_data', $step_info['ask_data']);
		$this->assign('step_data', $step_info['step_data']);
		$this->assign('step_num', $step_num);
		$this->assign('step_info', $step_info);
		$this->assign('agreement_id', $agreement_id);
		$this->assign('next_step', $next_step);
		$this->assign('cache_answer', $cache_answer);
		$this->assign('doc_content', $doc_content);
		$this->display();
	}
	// 获取预览数据
	public function get_preview_data(){
		$agreement_id = $this->_get('agreement_id', '对不起，您的操作有误！');
		// 更新数据
		if(IS_POST){
			$answer = $this->_post('answer');
			D('FreeAgreement')->set_ask_answer($this->user_id, $agreement_id, $answer);
		}
		$data['html'] = D('FreeAgreement')->get_doc_content($this->user_id, $agreement_id);
		$this->success('更新预览成功！', '', $data);
	}
	// 完成合同
	public function finish(){
		// 不显示头部导航
		$this->is_show_header_nav = 0;
		// 获取合同信息
		$agreement_id = $this->_get('agreement_id', '对不起，您的操作有误！');
		// 设置登录跳转地址
		session('login_jumpurl', U('finish', array('agreement_id' => $agreement_id)));

		$info = D('FreeAgreement')->info($agreement_id);
		$this->add_step($info['title']);
		$doc_content = D('FreeAgreement')->get_doc_content($this->user_id, $agreement_id);

		$this->assign('info', $info);
		$this->assign('doc_content', $doc_content);
		$this->display();
	}
	// 下载合同
	public function download(){
		// 登录用户才能下载
		$this->user_id();

		$agreement_id = $this->_get('agreement_id', '对不起，您的操作有误！');
		$order_sn     = $this->_get('order_sn');
		$doc_content  = D('FreeAgreement')->get_doc_content($this->user_id, $agreement_id, $order_sn);
		$doc_title    = D('FreeAgreement')->where(array('id' => $agreement_id))->getField('title');

		// 保存成订单
		if(!D('Order')->submit_free_order($this->user_id, $agreement_id)){
			$this->error(D('Order')->tips_info);
		}
		D('FreeAgreement')->where(array('id' => $agreement_id))->setInc('download_num');
		
		$doc_file = generate_doc($doc_title, $doc_content);
		header("Cache-Control: no-cache, must-revalidate"); 
		header("Pragma: no-cache"); 
		header("Content-Type: application/docx"); 
		header("Content-Disposition: attachment; filename=" . $doc_title . ".docx"); 
		readfile($doc_file);
		unlink($doc_file);
	}
	// 保存订单
	public function save_order(){
		// 登录用户才能保存订单
		$agreement_id = $this->_get('agreement_id', '对不起，您的操作有误！');
		$user_id      = $this->user_id();
		if($this->is_lawyer){
			$this->error('律师用户不能保存订单！');
		}

		if(D('Order')->submit_free_order($user_id, $agreement_id)){
			$this->success('保存成功！');
		}else{
			$this->error(D('Order')->tips_info);
		}
	}
	// 发送邮件
	public function send_email(){
		$agreement_id = $this->_get('agreement_id', '对不起，您的操作有误！');
		$user_id      = $this->user_id();
		$user_email   = $this->user_email;
		if(!$user_email){
			$this->error('对不起，您尚未配置E-mail地址！');
		}
		if(!$this->is_auth_email){
			$this->error('对不起，您的邮箱尚未认证');
		}
		$doc_title   = D('FreeAgreement')->where(array('id' => $agreement_id))->getField('title');
		$doc_content = D('FreeAgreement')->get_doc_content($this->user_id, $agreement_id);
		// 保存成订单
		if(!D('Order')->submit_free_order($this->user_id, $agreement_id)){
			$this->error(D('Order')->tips_info);
		}
		// 发送邮件
		if(D('Mail')->send_agreement_mail($doc_title, $doc_content, $user_email, $this->user_realname)){
			D('FreeAgreement')->where(array('id' => $agreement_id))->setInc('send_mail_num');
			$this->success('发送成功，请注意查收！');
		}else{
			$this->error('发送失败->' . D('Mail')->tips_info);
		}
	}



	// 专业版
	public function advanced(){
		$this->add_step('专业版');
		
		D('FreeAgreement')->pagesize = 8;
		$list  = D('FreeAgreement')->get_list('', 'save_num desc,download_num desc');

		$this->assign('list', $list);
		$this->display();
	}
}

